package com.ruoyi.business.controller;

import com.ruoyi.business.domain.bo.CommentBo;
import com.ruoyi.business.domain.vo.CommentVo;
import com.ruoyi.business.service.ICommentService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @author 最爱喝兽奶
 * @create 2022/7/30
 **/
@Api("评论的控制器")
@RestController
@RequestMapping("/business/comment")
public class CommentController extends BaseController {

    @Autowired
    private ICommentService commentService;


    @PreAuthorize("@ss.hasPermi('system:INFO:edit')")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PostMapping
    @ApiOperation("插入评论的操作")
    public AjaxResult insertComment(@RequestBody CommentBo bo) {
        if (null == bo.getCommentUser()) {
            // 设置评论人
            bo.setCommentUser(String.valueOf(getUserId()));
        }
        bo.setCreateTime(new Date());
        bo.setUpdateTime(new Date());
        bo.setCommentTime(new Date());
        commentService.insertComment(bo);
        return AjaxResult.success("评论成功", bo.getId());
    }


    @PreAuthorize("@ss.hasPermi('system:INFO:edit')")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PostMapping("/courseComment")
    @ApiOperation("插入归属于课程的评论的操作")
    public AjaxResult insertCommentForCourse(@RequestBody Map<String, Object> param) {
        CommentBo bo = new CommentBo();
        bo.setCommentContext(param.get("content").toString());
        if (null == bo.getCommentUser()) {
            // 设置评论人
            bo.setCommentUser("1");
//            bo.setCommentUser(getUserId());
        }
        bo.setCreateTime(new Date());
        bo.setUpdateTime(new Date());
        bo.setCommentTime(new Date());
        if (commentService.insertCommentForCourse(bo, param.get("courseId").toString())) {
            return AjaxResult.success(bo.getId());
        }else {
            return AjaxResult.error();
        }
    }



    @Log(title = "查询课程信息的评论")
    @GetMapping("/course/{courseId}")
    public AjaxResult courseList(@PathVariable(value = "courseId", required = true) String courseId,
                                 @RequestParam(value = "pageSize", defaultValue = "10") String pageSize,
                                 @RequestParam(value = "pageNum", defaultValue = "1") String pageNum) {
        TableDataInfo<CommentVo> result = commentService.queryCourseList(courseId, pageSize, pageNum);
        return AjaxResult.success(result);
    }

}
